﻿using System;
using System.Collections.Generic;
using System.Text;
using VIA.Integration.Automatics;
using VIA.Integration;
using System.Threading;
using VIA.ImageProcessing;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Extensions.DependencyInjection;
using VIA.Integration.Domain;

namespace VIA.Project.Domain.Devices
{
    public class VirtualProfileCamera : Automatic, IProfileCamera, IHasOption<VirtualCameraOption>, ISuportInitialization, IVirtualCamera
    {
        public bool IsConnected { get => true; }
        public VirtualCameraOption Option { get; set; }
        public AcquisitionMode AcquisitionMode { get; set; }
        public TriggerMode TriggerMode { get; set; }
        public TriggerSource TriggerSource { get; set; }
        public bool IsGrabbing { get; }
        public bool IsStart { get; }

        public event Action<IDevice> IsConnectedChanged;
        public event EventHandler<CameraIsGrabChangedArgs> IsGrabChanged;
        public event EventHandler<ProfileImageReceivedEventArgs> OnImageReceived;
        public event EventHandler<object> ImageObjectReceived;
        public event Action<IProfileCamera> IsStartChanged;

        public void Connect()
        {

        }

        public void Disconnect()
        {

        }

        public Task InitializeAsync()
        {
            Option = Service.GetService<ICurrentRecipe>().Get<VirtualCameraOption>(this.Id);
            if (Option == null)
                throw new Exception("Option不可以 为空");
            return Task.CompletedTask;
        }

        public ProfileImage Snap(CancellationToken cancellationToken = default, int timeout = 1000)
        {
            if (Option.ImagePath.IsNullOrEmpty()) return null;
            string tifFile = string.Empty;
            string pngFile = string.Empty;
            string bmpFile = string.Empty;
            if (Option.IsUseImageFolder)
            {
                if (!Directory.Exists(Option.ImagePath))
                {
                    return null;
                }
                Random random = new Random();
                string[] strs = Directory.GetFiles(Option.ImagePath, "*.png").Union(Directory.GetFiles(Option.ImagePath, "*.bmp")).ToArray();
                bmpFile = strs.Length == 0 ? null : strs[random.Next(strs.Length)];
                string[] strs2 = Directory.GetFiles(Option.ImagePath, "*.tif");
                tifFile = strs2.Length == 0 ? null : strs2[random.Next(strs2.Length)];
            }
            else
            {
                string fileWithoutEx = Option.ImagePath?.Substring(0, Option.ImagePath.Length - 4);
                tifFile = fileWithoutEx + ".tif";
                bmpFile = fileWithoutEx + ".bmp";
                pngFile = fileWithoutEx + ".png";
                if (!File.Exists(bmpFile) && File.Exists(pngFile))
                {
                    bmpFile = pngFile;
                }
            }
            ImageData luminum = bmpFile.IsNullOrEmpty() || !File.Exists(bmpFile) ? null : new ImageData(bmpFile) { Resolution = Option.ResolutionX };
            ImageData3DU depth = tifFile.IsNullOrEmpty() || !File.Exists(tifFile) ? null : new ImageData3DU(tifFile) { XResolution = Option.ResolutionX, YResolution = Option.ResolutionY, ZResolution = Option.ResolutionZ };
            ProfileImage image = new ProfileImage(luminum, depth);
            ImageObjectReceived?.Invoke(this, image);
            return image;
        }

        public object Snap()
        {
            return Snap(default, -1);
        }

        public void Start()
        {

        }

        public void Stop()
        {
        }
    }
}
